import React, {PureComponent} from "react";
import FormEditor from '../../components/FormEditor';
import {updateOrderModel} from "./actions";
import {connect} from "react-redux";

class OrderInfoModal extends PureComponent {

    handleUpdateCarrierModel = order => {
        const {dispatch, model} = this.props;
        dispatch(updateOrderModel({...model, ...order, "id": model.id}));
    };

    render() {
        const {model, clientList, organizations, coachList, businessCodeList, calculateType, cabinetTypeList, lines = [], goods, bgTypes} = this.props;
        const originDestPlaceList = [];
        model.originDestPlace = model.originPlace + "-" + model.destPlace;
        lines.forEach(item => {
            const originDestPlace = item.originPlace + "-" + item.destPlace;
            const found = originDestPlaceList.find(item => item.value === originDestPlace);
            if (!found) {
                originDestPlaceList.push({label: originDestPlace, value: originDestPlace});
            }
        });
        const schema = [
            {
                title: '基础信息',
                fields: [
                    {
                        field: 'clientId',
                        title: '客户',
                        type: 'listSelector',
                        readonly: model.status === "CONFIRMED",
                        controlProps: {
                            dataSource: clientList,
                            labelField: "name",
                            valueField: "id"
                        },
                        fieldOptions: {
                            rules: [{required: true, message: '请选择客户'}]
                        }
                    }, {
                        field: 'businessCode',
                        title: '业务类型',
                        type: 'listSelector',
                        readonly: model.status === "CONFIRMED",
                        controlProps: {
                            dataSource: businessCodeList,
                            labelField: "name",
                            valueField: "name"
                        },
                        fieldOptions: {
                            rules: [{required: true, message: '请选择业务类型'}]
                        }
                    }, {
                        field: 'calculateType',
                        title: '计费方式',
                        type: 'listSelector',
                        readonly: model.status === "CONFIRMED",
                        controlProps: {
                            dataSource: calculateType
                        },
                        fieldOptions: {
                            rules: [{required: true, message: '请选择计费方式'}]
                        }
                    }, {
                        field: 'bookName',
                        title: '订舱人',
                        type: 'text',
                        readonly: model.status === "CONFIRMED"
                    }, {
                        field: 'bookNumber',
                        title: '订舱号',
                        readonly: model.status === "CONFIRMED",
                        type: 'text'
                    }, {
                        field: 'pickDate',
                        title: '提货日期',
                        readonly: model.status === "CONFIRMED",
                        type: 'date'
                    }, {
                        field: 'pickPlace',
                        title: '提货地点',
                        readonly: model.status === "CONFIRMED",
                        type: 'text'
                    }, {
                        field: 'cabinetType',
                        title: '柜型',
                        type: 'autoComplete',
                        readonly: model.status === "CONFIRMED",
                        controlProps: {
                            dataSource: cabinetTypeList
                        }
                    }, {
                        field: 'preDepartureTime',
                        title: '班列日期',
                        readonly: model.status === "CONFIRMED",
                        type: 'date'
                    }, {
                        field: 'actDepartureTime',
                        title: '发车日期',
                        readonly: model.status === "CONFIRMED",
                        type: 'date'
                    }, {
                        field: 'quantity',
                        title: '数量',
                        readonly: model.status === "CONFIRMED",
                        type: 'number'
                    }, {
                        field: 'volume',
                        title: '货物体积(m³)',
                        readonly: model.status === "CONFIRMED",
                        type: 'number'
                    }, {
                        field: 'customsType',
                        title: '报关方式',
                        readonly: model.status === "CONFIRMED",
                        type: 'listSelector',
                        controlProps: {
                            dataSource: bgTypes,
                            labelField: "name",
                            valueField: "name"
                        }
                    }, {
                        field: 'goodsName',
                        title: '货物品类',
                        readonly: model.status === "CONFIRMED",
                        type: 'listSelector',
                        controlProps: {
                            dataSource: goods,
                            labelField: "name",
                            valueField: "name"
                        }
                    }, {
                        field: 'goodsDetailName',
                        title: '货物品名',
                        readonly: model.status === "CONFIRMED",
                        type: 'text'
                    }, {
                        field: 'goodsWeight',
                        title: '货物重量(KG)',
                        type: 'number',
                        readonly: model.status === "CONFIRMED"
                    }, {
                        field: 'originDestPlace',
                        title: '线路',
                        readonly: model.status === "CONFIRMED",
                        type: 'listSelector',
                        controlProps: {
                            dataSource: originDestPlaceList
                        },
                        fieldOptions: {
                            rules: [{required: true, message: '请选择线路'}]
                        }
                    }, {
                        field: 'carrierDeparturePort',
                        title: '出境口岸',
                        readonly: model.status === "CONFIRMED",
                        type: 'listSelector',
                        controlProps: {
                            dataSource: coachList,
                            labelField: "name",
                            valueField: "name"
                        }
                    }
                    /*, {
                        field: 'orderArriveTime',
                        title: 'ATA到达日期',
                        readonly: model.status === "CONFIRMED",
                        type: 'datetime'
                    }*/
                    , {
                        field: 'orderLeaveTime',
                        title: '境外提重日期',
                        readonly: model.status === "CONFIRMED",
                        type: 'datetime'
                    }, {
                        field: 'orderBeyondDays',
                        title: '超期天数(天)',
                        readonly: true
                    }, {
                        field: 'packageReturnDate',
                        title: '还箱日期',
                        readonly: model.status === "CONFIRMED",
                        type: 'date'
                    }, {
                        field: 'packageUseDays',
                        title: '用箱天数(天)',
                        readonly: true,
                        type: 'date'
                    },{
                        field: 'remark',
                        title: '备注',
                        type: 'textArea',
                        readonly: model.status === "CONFIRMED",

                    },
                    {
                        field: 'billRemark',
                        title: '账单备注',
                        type: 'textArea',
                        readonly: model.status === "CONFIRMED",

                    }
                ]
            }];
        schema[0].fields.splice(0, 0, {
            field: 'organizationId',
            title: '所属机构',
            type: 'treeSelector',
            readonly: model.status === "CONFIRMED",
            controlProps: {
                treeData: organizations,
                labelField: "name",
                valueField: "id",
                treeCheckable: false
            },
            fieldOptions: {
                rules: [{required: true, message: '请选择所属机构'}]
            }
        });
        return (
            <FormEditor
                schema={schema}
                column={3}
                defaultReadonly={true}
                showActionBar={false}
                defaultValues={model}
                onChange={this.handleUpdateCarrierModel}
            />
        );
    }
}

const mapStateToProps = state => {
    return {
        clientList: state.home.clients,
        carrierList: state.home.carriers,
        currencyList: state.zadOrderPayable.list.currencyList,
        businessCodeList: state.zadOrderPayable.list.businessCodeList,
        goods: state.goods.list.dataSource,
        coachList: state.zadOrder.list.coachList,
        calculateType: state.common.calculateType,
        bgTypes: state.bgType.list.dataSource,
        organizations: state.home.organizations,
        outFees: state.zadOrder.orderOutFee.feeList,
        lines: state.zadOrder.lines.lines
    };
};

export default connect(mapStateToProps)(OrderInfoModal);
